<?php
/**
 * Portfolio plugin intialization
 */

elgg_register_event_handler('init', 'system', 'xray_init');

/**
 * Initialize page handler and site menu item
 */
function xray_init() {
	elgg_unregister_menu_item('topbar', 'elgg_logo');

	elgg_register_page_handler('xray', 'xray_page_handler');
	elgg_register_page_handler('focus', 'focus_page_handler');

	$item = new ElggMenuItem('xray', elgg_echo('Security Perspective'), 'xray/000002');
	elgg_register_menu_item('site', $item);
			
	elgg_load_library('elgg:portfolio');
	elgg_load_library('elgg:misc');
}

/**
 * Portfolio page handler
 *
 * @param array $page url segments
 * @return bool
 */
function xray_page_handler($page) {
	
	$base = elgg_get_plugins_path() . 'xray/pages/xray';
	
	if ($page[0]=='data') {
		echo elgg_view('data', array('sec'=>$page[1]));
		exit;
	}
	if ($page[0]=='redirect') {
		$securities_param = get_param_value('securities_param', 'xray_securities_param', '');
		if (!$securities_param) {
			$securities_param[] = "000002";
		}	
		//print_r($securities_param);
		$_SESSION['xray_securities_param'] = $securities_param;
		
		$fund_param = get_param_value('fund_param', 'fund_param', '');
		$_SESSION['fund_param'] = $fund_param;
		
		forward('xray/'.$securities_param[0] . '?fund_param=' . $fund_param);
		exit;
	}
	$vars = array();
	$vars['page'] = $page[0];
	
/*
	if (!isset($page[0])) {
		$page[0] = 'newest';
	}

	if ($page[0] == 'search') {
		$vars['search_type'] = $page[1];
		require_once "$base/search.php";
	} else {
		require_once "$base/index.php";
	}
*/
	
	require_once "$base/index.php";
	
	return true;
}

function focus_page_handler($page) {

	if ($page[0]=='add') {
		echo elgg_view('focus/add');
		exit;
	}
	if ($page[0]=='del') {
		echo elgg_view('focus/del');
		exit;
	}

	return true;
}

